package com.shop.milk_tea_shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.milk_tea_shop.common.CodeEnum;
import com.shop.milk_tea_shop.common.GlobalInfo;
import com.shop.milk_tea_shop.common.PageInfo;
import com.shop.milk_tea_shop.common.Result;
import com.shop.milk_tea_shop.entity.EmployeeInfo;
import com.shop.milk_tea_shop.mapper.EmployeeInfoMapper;
import com.shop.milk_tea_shop.request.EmployeeQueryRequest;
import com.shop.milk_tea_shop.service.EmployeeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 职员信息表：	存储最高管理员admin、店长店员等职员信息。 服务实现类
 * </p>
 *
 * @author yjw
 * @since 2024-06-16
 */
@Service
@Slf4j
public class EmployeeInfoServiceImpl extends ServiceImpl<EmployeeInfoMapper, EmployeeInfo> implements EmployeeInfoService {

    @Autowired
    EmployeeInfoMapper employeeInfoMapper;

    @Override
    public Result<List<EmployeeInfo>> paged(EmployeeQueryRequest request) {
        LambdaQueryWrapper<EmployeeInfo> queryWrapper  = new LambdaQueryWrapper<>();

        queryWrapper.eq(request.getRole() != null, EmployeeInfo::getRole, request.getRole());
        queryWrapper.eq(request.getStatus() != null, EmployeeInfo::getStatus, request.getStatus());
        queryWrapper.like(StringUtils.isNotBlank(request.getEmployeeName()), EmployeeInfo::getEmployeeName, request.getEmployeeName());
        queryWrapper.eq(request.getEmployeeId() != null, EmployeeInfo::getEmployeeId, request.getEmployeeId());

        Page<EmployeeInfo> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<EmployeeInfo> pageResult = employeeInfoMapper.selectPage(page, queryWrapper);

        PageInfo pageInfo = new PageInfo(pageResult.getCurrent(), pageResult.getSize(), pageResult.getTotal());
        GlobalInfo globalInfo = new GlobalInfo(pageInfo);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, pageResult.getRecords(), globalInfo);
    }

    @Override
    public boolean updateById(EmployeeInfo entity) {
        employeeInfoMapper.updateById(entity);
        return true;
    }
}
